home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Form / Sources / Dialog.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  16.2 KB  |  487 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Dialog.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Laurent Delamare
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "Form.hpp"
  13.  
  14. #ifndef DIALOG_H
  15. #include "Dialog.h"
  16. #endif
  17.  
  18. #ifndef PART_H
  19. #include "Part.h"
  20. #endif
  21.  
  22. #ifndef FRAME_H
  23. #include "Frame.h"
  24. #endif
  25.  
  26. #ifndef FORMVIEW_H
  27. #include "FormView.h"
  28. #endif
  29.  
  30. #ifndef DEFINES_K
  31. #include "Defines.k"
  32. #endif
  33.  
  34. // ----- Framework Layer -----
  35.  
  36. #ifndef FWEDVIEW_H
  37. #include "FWEdView.h"
  38. #endif
  39.  
  40. #ifndef FWSTATIC_H
  41. #include "FWStatic.h"
  42. #endif
  43.  
  44. #ifndef FWUTIL_H
  45. #include "FWUtil.h"
  46. #endif
  47.  
  48. #ifndef FWCONTXT_H
  49. #include "FWContxt.h"
  50. #endif
  51.  
  52. #ifndef FWBUTTON_H
  53. #include "FWButton.h"
  54. #endif
  55.  
  56. #ifndef FWKEYF_H
  57. #include "FWKeyF.h"
  58. #endif
  59.  
  60. #ifndef FWNOTDEF_H
  61. #include "FWNotDef.h"
  62. #endif
  63.  
  64. // ----- OS Layer -----
  65.  
  66. #ifndef FWMENU_H
  67. #include "FWMenu.h"
  68. #endif
  69.  
  70. #ifndef FWEVENT_H
  71. #include "FWEvent.h"
  72. #endif
  73.  
  74. #ifndef FWALERT_H
  75. #include "FWAlert.h"
  76. #endif
  77.  
  78. #ifndef SLMixOS_H
  79. #include "SLMixOS.h"
  80. #endif
  81.  
  82. #ifndef FWCFMRES_H
  83. #include "FWCFMRes.h"
  84. #endif
  85.  
  86. // ----- Graphic Includes -----
  87.  
  88. #ifndef FWRECT_H
  89. #include "FWRect.h"
  90. #endif
  91.  
  92. #ifndef FWTXTBOX_H
  93. #include "FWTxtBox.h"
  94. #endif
  95.  
  96. #ifndef FWRECSHP_H
  97. #include "FWRecShp.h"
  98. #endif
  99.  
  100. // ----- Foundation Layer -----
  101.  
  102. #ifndef FWEXCDEF_H
  103. #include <FWExcDef.h>
  104. #endif
  105.  
  106. #ifndef FWNOTIFN_H
  107. #include "FWNotifn.h"
  108. #endif
  109.  
  110. //========================================================================================
  111. // Runtime Informations
  112. //========================================================================================
  113.  
  114. #ifdef FW_BUILD_MAC
  115. #pragma segment odfform
  116. #endif
  117.  
  118. FW_DEFINE_AUTO(CPwdDialogFrame)
  119. FW_DEFINE_AUTO(CPwdEditView)
  120.  
  121. //========================================================================================
  122. // class CPwdDialogFrame 
  123. //========================================================================================
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // CPwdDialogFrame::CPwdDialogFrame
  127. //----------------------------------------------------------------------------------------
  128. CPwdDialogFrame::CPwdDialogFrame(Environment* ev, ODFrame* odFrame, 
  129.                         FW_CPresentation* presentation,    CFormPart* part, FW_ResourceId id)
  130.     : FW_CDialogFrame(ev, odFrame, presentation, part, id) 
  131. {
  132.  
  133.     // If your modal dialog contains edit views which support Undo/Redo like CScrollEditView
  134.     // you should uncomment the following line to mark the Undo & Redo stacks
  135.     // This way you can clear the action history when the dialog is deleted (see below).    
  136. //    part->GetSession(ev)->GetUndo(ev)->MarkActionHistory(ev);
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // CPwdDialogFrame::~CPwdDialogFrame
  141. //----------------------------------------------------------------------------------------
  142.  
  143. CPwdDialogFrame::~CPwdDialogFrame()
  144. {
  145.     // If your modal dialog contains edit views which support Undo/Redo like CScrollEditView
  146.     // you should uncomment the following line to clear the Undo & Redo stacks
  147. //    GetPart(ev)->GetSession(ev)->GetUndo(ev)->ClearActionHistory(ev, kODRespectMarks);
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // CPwdDialogFrame::FacetAdded
  152. //----------------------------------------------------------------------------------------
  153. // On the Mac we need to add a color table to the dialog window in order to draw
  154. // a uniform gray background (otherwise the dialog frame keeps a white border)
  155.  
  156. #ifdef FW_BUILD_MAC
  157.  
  158. void CPwdDialogFrame::FacetAdded(Environment* ev, ODFacet* facet, unsigned short facetCount)
  159. {
  160.     FW_CDialogFrame::FacetAdded(ev, facet, facetCount);
  161.     
  162.     if (facetCount == 1)
  163.     {
  164.         ODPlatformWindow platformWindow = facet->GetWindow(ev)->GetPlatformWindow(ev);
  165.         
  166.         WCTabHandle newColorTable = (WCTabHandle)FW_CMemoryManager::AllocateSystemHandle
  167.                                                                     (8+sizeof(ColorSpec));
  168.         (*newColorTable)->ctSize = 0;
  169.         (*newColorTable)->ctTable[0].value = wContentColor;
  170.         (*newColorTable)->ctTable[0].rgb = FW_CColor(FW_kRGBLightGray);
  171.         ::SetWinColor(platformWindow, newColorTable);
  172.     }
  173. }
  174. #else
  175. //----------------------------------------------------------------------------------------
  176. // CPwdDialogFrame::Draw
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void CPwdDialogFrame::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)
  180. {
  181.     // Set drawing contect for the CFrame
  182.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  183.  
  184.     // Draw gray background
  185.     FW_CRectShape::RenderRect(vc, GetBounds(ev), FW_kFill, FW_kRGBLightGray);
  186.  
  187. }
  188. #endif // FW_BUILD_MAC
  189.  
  190. //----------------------------------------------------------------------------------------
  191. //    CPwdDialogFrame::PostCreateViewFromStream
  192. //----------------------------------------------------------------------------------------
  193. // PostCreateViewFromStream is called after subviews are created from resources.  
  194. // Implement initializations for this view that can't be done from a stream.
  195.  
  196. void CPwdDialogFrame::PostCreateViewFromStream(Environment* ev)
  197. {
  198.     // ----- Call inherited to propagate to all subviews if necessary
  199.     FW_CSuperView::PostCreateViewFromStream(ev);
  200.  
  201.     // ----- Add alpha-numeric keyfilters to edit views 
  202.     //        (will be deleted along with the edit views, they are adopted event handlers)
  203.     FW_CView* editView1 = FindViewById(ev, kPasswordEditID);    
  204.     FW_CView* editView2 = FindViewById(ev, kConfirmEditID);    
  205.     
  206.     FW_CAlphaNumFilter* keyF1 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView1);
  207.     FW_CAlphaNumFilter* keyF2 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView2);
  208.     
  209.     // Note: OK and Cancel buttons are linked automatically to the dialogFrame by
  210.     //         defining FW_kDefaultButtonMsg and FW_kCancelButtonMsg in "Views.fr"
  211.  
  212.     // WARNING: Make sure that classes created from resources won't be dead-stripped
  213.     //            (here we don't need to worry about our custom class CPwdEditView
  214.     //            because all its implementation is compiled in this file)
  215.     FW_DO_NOT_DEAD_STRIP(FW_CButton);
  216.     FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // CPwdDialogFrame::CreateSubViews
  221. //----------------------------------------------------------------------------------------
  222.  
  223. // NOTE: CreateSubViews is not needed when the frame's ctor is passed a resource id.
  224. //          This section was left in the sample to show how to create views by program
  225. //           instead of using resources. 
  226. #if 0
  227. void CPwdDialogFrame::CreateSubViews(Environment* ev)
  228. {        
  229.     FW_CRect viewRect;        // working rectangle for creating the views
  230.     
  231.     // ---- Create the static texts
  232.     viewRect.Set(FW_IntToFixed(20), FW_IntToFixed(40), FW_IntToFixed(140), FW_IntToFixed(70));
  233.     FW_CStaticText* staticText1 = FW_NEW(FW_CStaticText, (ev, this, viewRect, FW_CString("Pick a \rpassword:")));
  234.  
  235.     viewRect.Set(FW_IntToFixed(20), FW_IntToFixed(70), FW_IntToFixed(140), FW_IntToFixed(100));
  236.     FW_CStaticText* staticText2 = FW_NEW(FW_CStaticText, (ev, this, viewRect, FW_CString("Retype to \nverify:")));
  237.     
  238.     // ---- Create the text edit controls using our custom Edit Views
  239.     viewRect.Set(FW_IntToFixed(150), FW_IntToFixed(40), FW_IntToFixed(280), FW_IntToFixed(60));
  240.     CPwdEditView* editView1 = FW_NEW(CPwdEditView, (ev, this, kPasswordEditID, viewRect));
  241.  
  242.     viewRect.Set(FW_IntToFixed(150), FW_IntToFixed(70), FW_IntToFixed(280), FW_IntToFixed(90));
  243.     CPwdEditView* editView2 = FW_NEW(CPwdEditView, (ev, this, kConfirmEditID, viewRect));
  244.     
  245.     // ----- Add alpha-numeric keyfilters to edit views
  246.     FW_CAlphaNumFilter* keyF1 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView1);
  247.     FW_CAlphaNumFilter* keyF2 = new FW_CAlphaNumFilter(ev, (FW_MEventHandler*)editView2);
  248.     
  249.     // ----- Create the OK/Cancel buttons: 
  250.     //        - Use normal font instead of default system font
  251.     //        - Set the control message so that they are initialized as "default" and "cancel" 
  252.     //          dialog buttons  (no need to call SetDefaultButton() and SetCancelButton() later)
  253.     viewRect.Set(FW_IntToFixed(180), FW_IntToFixed(117), FW_IntToFixed(260), FW_IntToFixed(148));
  254.     FW_CButton* okButton = FW_NEW(FW_CButton, (ev, this, kOKButtonID, viewRect, FW_kPushButton, 
  255.                                         FW_CString("OK"), FW_kNormalFont, FW_kDefaultButtonMsg));
  256.  
  257.     viewRect.Set(FW_IntToFixed(60), FW_IntToFixed(123), FW_IntToFixed(140), FW_IntToFixed(145));
  258.     FW_CButton* cancelButton = FW_NEW(FW_CButton, (ev, this, kCancelButtonID, viewRect, FW_kPushButton, 
  259.                                         FW_CString("Cancel"), FW_kNormalFont, FW_kCancelButtonMsg));
  260. }
  261. #endif
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // CPwdDialogFrame::Initialize
  265. //----------------------------------------------------------------------------------------
  266. // You can group all additional dialog initialization in a local method like this one
  267. // or simply put the code after your call to FW_CDialogFrame::NewModalDialog 
  268.  
  269. void CPwdDialogFrame::Initialize(Environment* ev, CFormFrame* frame)
  270. {
  271.     fFormFrame = frame;
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    CPwdDialogFrame::HandleNotification
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void CPwdDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  279. {
  280.     // ---- Verify edit fields when clicking on OK button ----
  281.     if (notification.GetMessage() ==  FW_kDefaultButtonMsg) 
  282.     {
  283.         // Get the 2 edit views
  284.         FW_CView* view =  FindViewById(ev, kPasswordEditID);
  285.         CPwdEditView* pwdTEd = FW_DYNAMIC_CAST(CPwdEditView, view);
  286.         FW_ASSERT(pwdTEd);
  287.         view =  FindViewById(ev, kConfirmEditID);
  288.         CPwdEditView* confirmTEd = FW_DYNAMIC_CAST(CPwdEditView, view);
  289.         FW_ASSERT(confirmTEd);
  290.  
  291.         // Check that the password has at least 3 characters
  292.         FW_CString text = pwdTEd->GetText(ev);
  293.         
  294.         if (text.GetCharacterLength() < 3) {
  295.             // Open Alert and return to avoid closing the dialog
  296.             FW_ErrorAlert(FW_CString(), FW_CString("Password must contain at least 3 characters"));
  297.             return;
  298.         }
  299.         
  300.         // Check that both strings are equal
  301.         FW_CString text2 = confirmTEd->GetText(ev);
  302.         
  303.         if (text != text2) {
  304.             // Open Alert and return to avoid closing the dialog
  305.             FW_ErrorAlert(FW_CString(), FW_CString("Please re-type, strings don't match"));
  306.             return;
  307.         }
  308.         
  309.         // Password is OK, we can switch the form. 
  310.         CFormView* formView = (CFormView*) fFormFrame->GetContentView(ev);
  311.         formView->SwitchForm(ev);
  312.     } 
  313.     
  314.     // Must call inherited to close the dialog on OK & Cancel
  315.     // WARNING: do not do anything else after this call... the dialog is gone!
  316.     FW_CDialogFrame::HandleNotification(ev, notification);
  317. }
  318.  
  319. //========================================================================================
  320. // class CPwdEditView 
  321. //========================================================================================
  322.  
  323. FW_DEFINE_CLASS_M1(CPwdEditView, FW_CEditView);
  324.  
  325. const FW_ClassTypeConstant LPwdEditView = FW_TYPE_CONSTANT('P','w','d','v');
  326. FW_REGISTER_ARCHIVABLE_CLASS(LPwdEditView, CPwdEditView, CPwdEditView::Create, FW_CView::Read, CPwdEditView::Destroy, FW_CView::Write)
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // CPwdEditView::CPwdEditView
  330. //----------------------------------------------------------------------------------------
  331. CPwdEditView::CPwdEditView(Environment* ev, FW_CSuperView* container, ODID  viewId,
  332.                                 const FW_CRect& bounds)
  333.     : FW_CEditView(ev, container, viewId, bounds) 
  334. {
  335.     Initialize(ev, container, bounds);
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. // CPwdEditView::CPwdEditView
  340. //----------------------------------------------------------------------------------------
  341. CPwdEditView::CPwdEditView(Environment* ev)
  342.     : FW_CEditView(ev) 
  343. {
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. // CPwdEditView::~CPwdEditView
  348. //----------------------------------------------------------------------------------------
  349.  
  350. CPwdEditView::~CPwdEditView()
  351. {
  352. }
  353.  
  354. //----------------------------------------------------------------------------------------
  355. // CPwdEditView::Initialize
  356. //----------------------------------------------------------------------------------------
  357.  
  358. void CPwdEditView::Initialize(Environment* ev, FW_CSuperView* container, const FW_CRect& bounds)
  359. {
  360.     FW_CRect farBounds(bounds);
  361.     farBounds.Place(FW_IntToFixed(30000), FW_IntToFixed(30000));
  362.  
  363.     // Create a shadow edit view to hold the real text
  364.     // (Note: Making the view invisible is not enough to hide it because the Mac toolbox
  365.     //  will still draw the text being inserted with TEKey) 
  366.     fShadowTEd = FW_NEW(FW_CEditView, (ev, container, kNoIdentifier, farBounds));
  367.     fShadowTEd->Disable(ev);
  368. }
  369.  
  370. //----------------------------------------------------------------------------------------
  371. //    CPwdEditView::Create
  372. //----------------------------------------------------------------------------------------
  373.  
  374. void* CPwdEditView::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  375. {
  376. FW_UNUSED(stream);
  377. FW_UNUSED(type);
  378.     FW_SOMEnvironment ev;
  379.     return FW_NEW(CPwdEditView, (ev));
  380. }
  381.  
  382. //----------------------------------------------------------------------------------------
  383. //    CPwdEditView::Destroy
  384. //----------------------------------------------------------------------------------------
  385.  
  386. void CPwdEditView::Destroy(void* object, FW_ClassTypeConstant type)
  387. {
  388. FW_UNUSED(type);
  389.     CPwdEditView* self = (CPwdEditView*) object;
  390.     delete self;
  391. }
  392.  
  393. //----------------------------------------------------------------------------------------
  394. //    CPwdEditView::Flatten
  395. //----------------------------------------------------------------------------------------
  396.  
  397. void CPwdEditView::Flatten(Environment* ev, FW_CWritableStream& archive) const
  398. {
  399.     FW_CEditView::Flatten(ev, archive);
  400. }
  401.  
  402. //----------------------------------------------------------------------------------------
  403. //    CPwdEditView::InitializeFromStream
  404. //----------------------------------------------------------------------------------------
  405.  
  406. void CPwdEditView::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  407. {
  408.     FW_CEditView::InitializeFromStream(ev, stream);
  409.     
  410.     Initialize(ev, GetSuperView(ev), GetBounds(ev));
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. // CPwdEditView::ActivateTarget
  415. //----------------------------------------------------------------------------------------
  416.  
  417. void CPwdEditView::ActivateTarget (Environment * ev, FW_Boolean tabSelection)
  418. {
  419.     // must select also the shadow TEd after a Tab
  420.     if (tabSelection)
  421.         fShadowTEd->SelectAll(ev);
  422.     
  423.     FW_CEditView::ActivateTarget(ev, tabSelection);
  424. }    
  425.  
  426. //----------------------------------------------------------------------------------------
  427. // CPwdEditView::DoMenu
  428. //----------------------------------------------------------------------------------------
  429.  
  430. FW_Boolean CPwdEditView::DoMenu (Environment* ev, const FW_CMenuEvent& event)
  431. {
  432.     switch (event.GetCommandID(ev)) {
  433.         case kODCommandCut: 
  434.         case kODCommandCopy: 
  435.         case kODCommandPaste: 
  436.         case kODCommandClear: 
  437.             return TRUE;
  438.         case kODCommandSelectAll: 
  439.             SelectAll(ev);
  440.             break;
  441.         default:
  442.             return FW_CView::DoMenu (ev, event);
  443.     }
  444.     return TRUE;
  445. }
  446.  
  447. //----------------------------------------------------------------------------------------
  448. // CPwdEditView::DoCharKey
  449. //----------------------------------------------------------------------------------------
  450.  
  451. FW_Boolean CPwdEditView::DoCharKey (Environment* ev, const FW_CCharKeyEvent & event)
  452. {
  453. #ifdef FW_BUILD_MAC
  454.     char bullet = 0xA5;
  455.  
  456.     if (event.GetModifiers() & cmdKey)
  457.         return false;
  458. #endif
  459. #ifdef FW_BUILD_WIN
  460.     char bullet = '*';
  461. #endif
  462.  
  463.     // For printable chars use bullet in visible TEd instead of real char
  464.     char realChar = event.GetChar(ev);
  465.     char pwdChar = (realChar >= 0x20) ? bullet : realChar;
  466.     
  467.     // Make Backspace erase the whole text
  468.     if (realChar == 0x08) {
  469.         SelectAll(ev);
  470.         fShadowTEd->SelectAll(ev);
  471.     }
  472.     
  473.     InputCharKey(ev, pwdChar); 
  474.     return fShadowTEd->InputCharKey(ev, realChar);
  475. }
  476.  
  477. //----------------------------------------------------------------------------------------
  478. // CPwdEditView::GetText
  479. //----------------------------------------------------------------------------------------
  480.  
  481. FW_CString CPwdEditView::GetText (Environment * ev)
  482. {
  483.     return fShadowTEd->GetText(ev);
  484. }
  485.  
  486.  
  487.